/************************************************************************************************
 * test examples of 100 interesting program in C
 * test 055.c
 * duty of doctors
 ***********************************************************************************************/

#include <stdio.h>
#include <string.h>

/*
 * in this program, i will test a method to using backtrace,
 * which is also aiming to generate a 7-digit 7-to-carry system.
 * but timing shows that it's too slow, which is obvious.
 *
 *
 * the advantage is that it can replace most number-to-carry system
 * even 0-to-carry system (in fact, it's useless, but exist). bcz
 * we can start from -1 as follow.
 *
 * we can use full arrangement algorithm to get better efficiency
 */

#define DOC_A 0
#define DOC_B 1
#define DOC_C 2
#define DOC_D 3
#define DOC_E 4
#define DOC_F 5
#define DOC_G 6

#define MON 0
#define TUE 1
#define WED 2
#define THU 3
#define FRI 4
#define SAT 5
#define SUN 6

#define N2C 7
#define DIGITS 7

int main()
{
	int duty[N2C];
	int doctor[DIGITS];

	int k = 0;
	for (k = 0; k < DIGITS; k++)
		doctor[k] = -1;

	int p = 0;
	int fin = 0;
	while (1)
	{
		while (fin != 1 && p < DIGITS)
		{
			if (doctor[p] == N2C)
				if (p == 0)
					fin = 1;
				else
					doctor[p--] = -1;
			else
				doctor[p++]++;
		}

		if (fin == 1) break;
		else
		{
			// check same digit
			int i = 0, flag = 1;
			memset(duty, 0, sizeof(int)*N2C);
			for (i = 0; i < DIGITS; i++)
			{
				if (duty[doctor[i]] == 1)
				{
					flag = 0;
					break;
				}
				else
				{
					duty[doctor[i]] = 1;
				}
			}

			// check relationship
			if (flag == 1)
			{
				if (
					doctor[DOC_F] == THU && 
					((doctor[DOC_B] < doctor[DOC_F] && doctor[DOC_C] > doctor[DOC_F])||
					(doctor[DOC_B] > doctor[DOC_F] && doctor[DOC_B] < doctor[DOC_F])) &&
					(doctor[DOC_A] - doctor[DOC_C] + 7) % 7 == 1 &&
					(doctor[DOC_D] - doctor[DOC_E] + 7) % 7 == 2 &&
					(doctor[DOC_G] - doctor[DOC_B] + 7) % 7 == 3
					)
				{
					int j = 0;
					for (j = 0; j < DIGITS; j++)
						printf("%3d", doctor[j]);
					printf("\n");
				}
			}
			p = DIGITS-1;
		}
	}
}

